package com.lanou.wuyou.gift.category;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.CategoryProductBean;

/**
 * Created by dllo on 17/3/18.
 */

public class CategoryProductLeftAdapter extends BaseAdapter{
    private CategoryProductBean bean;
    private Context context;
    private int selectIndex;

    public void setBean(CategoryProductBean bean) {
        this.bean = bean;
        notifyDataSetChanged();
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
        notifyDataSetChanged();
    }

    public CategoryProductLeftAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (bean == null){
            return 0;
        }
        if (bean.getData() == null){
            return 0;
        }
        return bean.getData().getCategories() != null && bean.getData().getCategories().size() > 0 ?
                bean.getData().getCategories().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return bean.getData().getCategories().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category_product_left,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (bean != null){
            CategoryProductBean.DataBean.CategoriesBean channelGroupsBean = bean.getData().getCategories().get(position);
            if (channelGroupsBean != null){
                viewHolder.textView.setText(channelGroupsBean.getName());
            }
        }
        if (position == selectIndex){
            viewHolder.textView.setBackgroundColor(Color.WHITE);
            viewHolder.textView.setTextColor(Color.parseColor("#ED2D44"));
            viewHolder.linearLayout.setBackgroundColor(Color.parseColor("#ED2D44"));
        }else {
            viewHolder.textView.setTextColor(Color.BLACK);
            viewHolder.textView.setBackgroundColor(Color.parseColor("#EDEDED"));
            viewHolder.linearLayout.setBackgroundColor(Color.WHITE);
        }
        return convertView;
    }


    class ViewHolder{
        TextView textView;
        LinearLayout linearLayout;
        View image;
        public ViewHolder(View view){
            textView = (TextView) view.findViewById(R.id.list_view_category_left);
            linearLayout = (LinearLayout) view.findViewById(R.id.linear_layout_view);
            image = view.findViewById(R.id.view_category_product_left);
        }
    }
}
